package cn.xz.gobang.util

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.view.View
import android.view.animation.*


/**
 * 动画合集*/
object AniUtil {

    //上下动画
    fun onSX(time: Long,view: View){
        val downAnim = ObjectAnimator.ofFloat(view, "translationY", 0F, 10F, 0F)
        val upAnim = ObjectAnimator.ofFloat(view, "translationY", 0F, -10F, 0F)
        downAnim.duration = time
        upAnim.duration = time
        downAnim.interpolator = LinearInterpolator()
        upAnim.interpolator = LinearInterpolator()
        downAnim.addListener(object : AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                // 向下动画结束，启动向上动画
                upAnim.start()
            }
        })
        upAnim.addListener(object :AnimatorListenerAdapter(){
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                // 向上动画结束，启动向下动画
                downAnim.start()
            }
        })
        // 先启动一次
        downAnim.start()
    }

    //放大缩小动画
    fun onDX(time: Long,view: View){
        val animation = ScaleAnimation(
            1f,
            1.2f,
            1f,
            1.2f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        animation.duration = time
        animation.fillAfter = true
        animation.repeatMode = Animation.REVERSE
        animation.repeatCount = 20

        val set = AnimationSet(true)
        set.addAnimation(animation)
        view.startAnimation(set)
    }

    fun animScaleIn(view: View?) {
        //缩放动画
        val animation = ScaleAnimation(
            1f,
            1.2f,
            1f,
            1.2f,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        )
        animation.duration = 100
        animation.fillAfter = true
        animation.repeatMode = Animation.REVERSE
        animation.repeatCount = 1

        //透明度动画
        val animation1 = AlphaAnimation(1f, 0.8f)
        animation1.duration = 100
        animation1.repeatCount = 1
        animation1.repeatMode = Animation.REVERSE
        animation1.fillAfter = true

        //装入AnimationSet中
        val set = AnimationSet(true)
        set.addAnimation(animation)
        set.addAnimation(animation1)
        view?.startAnimation(set)
    }
}